/** ------------------------------------------------------------------------------------------------------
@file myname.c
@author genegong@qq.com
@date 2019-10-22
@version 0.0.3
@brief This is a sample UDF for MySQL. It returns the library version.
@details This UDF is used to test the installation and configuration of the MySQL UDF library.
@copyright Copyright (c) 2019
@license This project is released under the MIT License.    
@see https://github.com/jimmy-src/mysql_udf_sys_info
@see https://dev.mysql.com/doc/refman/8.0/en/creating-a-stored-function.html


## how to compile
rm myname.def myname.dll
D:\dev\tcc\tcc.exe -ID:\dev\mariadb\include\mysql -shared .\myname.c
cp myname.dll D:\dev\mariadb\lib\plugin

## create Mysql/ Mariadb Function
DROP FUNCTION IF EXISTS myname;
CREATE FUNCTION myname RETURNS string SONAME 'myname.dll';

## test 
SELECT myname();

 ------------------------------------------------------------------------------------------------------ */
#include <mysql.h>
#include <string.h>
#include <stdio.h>

#ifdef _WIN32   // Windows system
#define DLLEXP __declspec(dllexport)
#else           // Linux system
#define DLLEXP
#endif

#ifdef __cplusplus
extern "C" {
#endif

#define LIBVERSION "lib myname version 0.0.3"
DLLEXP my_bool myname_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void myname_deinit(UDF_INIT *initid);
DLLEXP char* myname(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error);


/**
 * function myname_init
 * @param initid
 * @param args
 * @param message   
 */
my_bool myname_init( UDF_INIT *initid,UDF_ARGS *args,char *message)
{
 my_bool status;
 if(args->arg_count!=0){
  strcpy(message,"No arguments allowed (udf: myname)");
  status = 1;
 } else {
  status = 0;
 }
 return status;
}

/**
 * function uninitialize
 * @param initid
 */ 
void myname_deinit(UDF_INIT *initid)
{
 return;
}
/**
 * function myname
 * @param initid
 * @param args
 * @param result
 * @param length
 * @param is_null
 * @param error
 */
char* myname(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error)
{
 strcpy(result,LIBVERSION);
 *length = strlen(LIBVERSION);
 return result;
}

#ifdef __cplusplus
}
#endif
